package com.ruoyi.system.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class pythonUtil {

    public static List<String> executeCMD(final String[] cmdStrArr) {
        Runtime rt = Runtime.getRuntime();
        System.out.println("开始执行脚本");
        System.out.println("脚本内容为:" + cmdStrArr);
            final List<String> stockList =new ArrayList<>();
        try {
            final Process p = rt.exec(cmdStrArr);
            // 防止缓冲区满, 导致卡住
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    String line;
                    try {
                        BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream(),"gbk"));
                        while ((line = stderr.readLine()) != null) {
                           System.out.println("stderr:" + line);
                        }
                    }
                    catch (Exception e) {

                    }

                }
            }.start();


            new Thread() {
                @Override
                public void run() {
                    super.run();
                    String line;
                    try {
                        BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
                        while ((line = stdout.readLine()) != null) {
                            stockList.add(line);
                        }
                    }
                    catch (Exception e) {

                    }
                }
            }.start();


            int exitVal = p.waitFor();
            if (0 != exitVal) {
                System.out.println("执行脚本失败");
            }
            System.out.println("执行脚本成功");

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return stockList;
    }
}
